/*
 	sqlite_wrapper.cc

 	Copyright (c) 2008 Bojan Rajkovic

 	Permission is hereby granted, free of charge, to any person obtaining a copy
 	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 	copies of the Software, and to permit persons to whom the Software is
 	furnished to do so, subject to the following conditions:
	
 	The above copyright notice and this permission notice shall be included in
 	all copies or substantial portions of the Software.

 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 	THE SOFTWARE.
*/

#include "../include/sqlite_wrapper.h"

sqlite_wrapper::sqlite_wrapper (const char* databaseName) {
	databasePath = databaseName;
	retval = sqlite3_open(databasePath, &db);
	if( retval ) {
		asprintf(&logMessage, _("Can't open database: %s, won't pulse to DB."), sqlite3_errmsg(db));
		utility::log_message(logMessage, M_ERROR);
		sqlite3_close(db);
		database_open_failed_exception could_not_open_pulsedb_ex;
		throw could_not_open_pulsedb_ex;
	}
	db_open = 1;
}

char* sqlite_wrapper::get_last_error() {
	return (char*)sqlite3_errmsg(db);
}

int sqlite_wrapper::table_exists(const char* tableName) {
	if (db_open == 0) {
		return -1;
	} else {
		int retval;
		char* sql;
		asprintf(&sql, "select count(*) from sqlite_master where type='table' and name='%s';", tableName);
		retval = exec(sql);
		if (retval == 0) {
			if (atoi(vdata.at(0).c_str()) > 0) return 1;
			else return 0;
		} else return retval;
	}
}

int sqlite_wrapper::exec(const char* sql) {
	retval = sqlite3_get_table(
		db,              /* An open database */
  		sql,      /* SQL to be executed */
  		&result,       /* Result written to a char *[]  that this points to */
  		&nrow,             /* Number of result rows written here */
  		&ncol,          /* Number of result columns written here */
  		&zErrMsg          /* Error msg written here */
  	);

	// Clear the previous data values
	if(vcol_head.size()<0) { vcol_head.clear();  }
	if(vdata.size()<0)     { vdata.clear(); }

	// if the return value is okay, let's fill the data out
	if( retval == SQLITE_OK ){
		for(int i=0; i < ncol; ++i) {
  			vcol_head.push_back(result[i]);   /* First row heading */
  		}
        	for(int i=0; i < ncol*nrow; ++i) {
  			vdata.push_back(result[ncol+i]);					
  		}
       	} else {
	       	char* message;
	       	asprintf(&message, _("Something went wrong with SQLite in sqlite3_get_table, this is the error sqlite3 gave us: %s"), zErrMsg);
	       	utility::log_message(message, M_ERROR);
       	}

       	sqlite3_free_table(result);
	return retval;
}

sqlite_wrapper::~sqlite_wrapper(){
	sqlite3_close(db);
}
